$import("com.joypard.platform.dict.DictDI");

/**
 * 用户
 */
K.defView("com.joypard.platform.dict.DictView", {
	dictCtrl : null, // 页面控制器
	initialize : function() {
		E.addListener("字典-新增-成功", this.onAddDictSuccessEvent, this);
		E.addListener("字典-修改-成功", this.onUpdateDictSuccessEvent, this);
		E.addListener("字典-删除-成功", this.onDelDictSuccessEvent, this);
	},
	render : function() {
		var me = this;
		this.westDictTreePanel = this.createWestDictTreePanel();
		this.centerDictEditPanel = this.createCenterDictEditPanel();

		this.westDictTreePanel.getSelectionModel().on("selectionchange", function(sm, node) {
			if (node) {
				me.loadEditPanel(DictDI.urls.updateDict, {
					dictId : node.id
				});
			}
			me.setButtonEnable();
		});

		var root = this.westDictTreePanel.getRootNode();
		root.expand(false, true, function() {
			root.select();
		});

		this.MP = new K.Ext.MainPanel({
			items : [ this.westDictTreePanel, this.centerDictEditPanel ]
		});
	},
	/**
	 * 左侧字典面板
	 */
	createWestDictTreePanel : function() {
		var options = {
			region : "west",
			title : "字典管理",
			width : "20%",
			iconCls : "p-panel",
			margins : "2 2 2 2",
			frame : true,
			autoScroll : true,
			loader : DictDI.createDictTreeLoader(),
			root : DictDI.createDictRootAsyncTreeNode()
		};

		return new K.Ext.tree.TreePanel(options);
	},
	/**
	 * 创建用户搜索面板
	 */
	createCenterDictEditPanel : function() {
		var options = {
			region : "center",
			title : "资源基本信息",
			iconCls : "p-edit",
			margins : "2 2 2 0",
			tbar : [ {
				id : "addDicBtn",
				text : "添加子字典",
				iconCls : 'add',
				handler : K.createDelegate(this.onAddViewDict, this)
			}, "-", {
				id : "delDicBtn",
				text : "删除字典",
				iconCls : 'delete',
				handler : K.createDelegate(this.onDelDict, this)
			}, "-", {
				id : "saveDicBtn",
				text : "保存字典",
				iconCls : 'save',
				handler : this.onSaveDict
			} ]
		};

		return new K.Ext.Panel(options);
	},

	/** ------ ------ ------ ------ 事件 ------ ------ ------ ------ */

	/**
	 * 单击[添加子字典]按钮
	 */
	onAddViewDict : function() {
		var selNode = this.westDictTreePanel.getSelectionModel().getSelectedNode();
		this.loadEditPanel(DictDI.urls.addDict, {
			parentDictId : selNode.id
		});
		this.setButtonEnable("add");
	},

	/**
	 * 点击[删除]按钮
	 */
	onDelDict : function() {
		var selNode = this.westDictTreePanel.getSelectionModel().getSelectedNode();
		if (selNode) {
			if (selNode.id != "0") {
				if (confirm("删除字典同时会删除子字典,您确定要删除吗?")) {
					E.fireEvent("字典-删除", selNode.id);
				}
			} else {
				K.err("不能删除根字典", this.westDictTreePanel.tbar);
			}
		}
	},

	/**
	 * 删除成功返回
	 */
	onDelDictSuccessEvent : function(output) {
		var dictId = output.getParameter("dictId");
		K.Ext.flyTree(this.westDictTreePanel).autoSelectByDelete(dictId);
	},

	onSaveDict : function() {
		var form = $("#dicForm"), state = form.attr("state"), eventName = null;
		if ("add" == state) {
			eventName = "字典-新增";
		} else if ("update" == state) {
			eventName = "字典-修改";
		}
		if (eventName) {
			E.fireEvent(eventName, form);
		}
	},

	/**
	 * 新增字典成功后事件
	 */
	onAddDictSuccessEvent : function(output) {
		var dict = output.getParameter("dict"), parent = K.Ext.flyTree(this.westDictTreePanel).getNodeById(dict.parent);
		dict.text = "【" + dict.code + "】" + dict.name;
		dict.leaf = true;
		dict.children = null; // delete !!!!!!!!
		parent.appendChild(new K.Ext.tree.AsyncTreeNode(dict));
		K.m("新增字典成功").show(this.centerDictEditPanel.tbar);
		this.setButtonEnable();
		this.loadEditPanel(DictDI.urls.addDict, {
			parentDictId : parent.id
		});
	},

	/**
	 * 更新字典成功后事件
	 */
	onUpdateDictSuccessEvent : function(output) {
		var dict = output.getParameter("dict");
		K.Ext.flyTree(this.westDictTreePanel).setNodeText(dict.id, "【" + dict.code + "】" + dict.name);
		K.m("修改字典成功").show(this.centerDictEditPanel.tbar);
	},

	/**
	 * 设置主面板上功能的按钮的可用状态
	 */
	setButtonEnable : function(op) {
		switch (op) {
		case "add": // add
			Ext.getCmp("addDicBtn").disable();
			Ext.getCmp("delDicBtn").disable();
			break;
		case "del": // delete
			Ext.getCmp("addDicBtn").disable();
			Ext.getCmp("saveDicBtn").disable();
			break;
		default:
			Ext.getCmp("addDicBtn").enable();
			Ext.getCmp("delDicBtn").enable();
			Ext.getCmp("saveDicBtn").enable();
		}
	},
	loadEditPanel : function(url, params) {
		this.centerDictEditPanel.load({
			method : "GET",
			url : url,
			params : params
		});
	}
});
